package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysAuthorizationMapper;
import com.ruoyi.system.domain.SysAuthorization;
import com.ruoyi.system.service.ISysAuthorizationService;

/**
 * 系统授权Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-15
 */
@Service
public class SysAuthorizationServiceImpl implements ISysAuthorizationService 
{
    @Autowired
    private SysAuthorizationMapper sysAuthorizationMapper;

    /**
     * 查询系统授权
     * 
     * @param authId 系统授权主键
     * @return 系统授权
     */
    @Override
    public SysAuthorization selectSysAuthorizationByAuthId(Long authId)
    {
        return sysAuthorizationMapper.selectSysAuthorizationByAuthId(authId);
    }

    /**
     * 根据授权码查询系统授权
     * 
     * @param authCode 授权码
     * @return 系统授权
     */
    @Override
    public SysAuthorization selectSysAuthorizationByAuthCode(String authCode)
    {
        return sysAuthorizationMapper.selectSysAuthorizationByAuthCode(authCode);
    }

    /**
     * 查询系统授权列表
     * 
     * @param sysAuthorization 系统授权
     * @return 系统授权
     */
    @Override
    public List<SysAuthorization> selectSysAuthorizationList(SysAuthorization sysAuthorization)
    {
        return sysAuthorizationMapper.selectSysAuthorizationList(sysAuthorization);
    }

    /**
     * 验证授权码是否有效
     * 
     * @param authCode 授权码
     * @return 验证结果
     */
    @Override
    public boolean validateAuthorizationCode(String authCode)
    {
        if (StringUtils.isEmpty(authCode))
        {
            return false;
        }

        SysAuthorization authorization = sysAuthorizationMapper.selectSysAuthorizationByAuthCode(authCode);
        
        if (authorization == null)
        {
            return false;
        }

        // 检查授权状态
        if (!"0".equals(authorization.getStatus()))
        {
            return false;
        }

        Date now = new Date();

        // 检查开始时间
        if (authorization.getStartTime() != null && now.before(authorization.getStartTime()))
        {
            return false;
        }

        // 如果是永久授权，直接返回true
        if ("1".equals(authorization.getIsPermanent()))
        {
            return true;
        }

        // 检查过期时间
        if (authorization.getExpireTime() != null && now.after(authorization.getExpireTime()))
        {
            return false;
        }

        return true;
    }

    /**
     * 检查系统是否已授权
     * 
     * @return 是否已授权
     */
    @Override
    public boolean isSystemAuthorized()
    {
        List<SysAuthorization> validAuths = sysAuthorizationMapper.selectValidAuthorizations();
        return validAuths != null && !validAuths.isEmpty();
    }

    /**
     * 获取当前有效的授权信息
     * 
     * @return 授权信息
     */
    @Override
    public SysAuthorization getCurrentValidAuthorization()
    {
        List<SysAuthorization> validAuths = sysAuthorizationMapper.selectValidAuthorizations();
        if (validAuths != null && !validAuths.isEmpty())
        {
            return validAuths.get(0);
        }
        return null;
    }

    /**
     * 新增系统授权
     * 
     * @param sysAuthorization 系统授权
     * @return 结果
     */
    @Override
    public int insertSysAuthorization(SysAuthorization sysAuthorization)
    {
        sysAuthorization.setCreateTime(DateUtils.getNowDate());
        return sysAuthorizationMapper.insertSysAuthorization(sysAuthorization);
    }

    /**
     * 修改系统授权
     * 
     * @param sysAuthorization 系统授权
     * @return 结果
     */
    @Override
    public int updateSysAuthorization(SysAuthorization sysAuthorization)
    {
        sysAuthorization.setUpdateTime(DateUtils.getNowDate());
        return sysAuthorizationMapper.updateSysAuthorization(sysAuthorization);
    }

    /**
     * 批量删除系统授权
     * 
     * @param authIds 需要删除的系统授权主键
     * @return 结果
     */
    @Override
    public int deleteSysAuthorizationByAuthIds(Long[] authIds)
    {
        return sysAuthorizationMapper.deleteSysAuthorizationByAuthIds(authIds);
    }

    /**
     * 删除系统授权信息
     * 
     * @param authId 系统授权主键
     * @return 结果
     */
    @Override
    public int deleteSysAuthorizationByAuthId(Long authId)
    {
        return sysAuthorizationMapper.deleteSysAuthorizationByAuthId(authId);
    }
}
